package com.itcrx.datastructure;

/**
 * @author NoobMaster69  * @date 2024/6/16 0016 星期日 14:01
 */

import java.util.Iterator;

/**
 * 双向链表（带哨兵）
 */
public class DoubleLinkedListSentinel implements Iterable<Integer> {
    static class Node {
        Node prev;  //上一个节点指针
        int value;  //值
        Node next;  //下一个节点指针

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }

    }

    private Node head;  //头节点
    private Node tail;  //尾节点

    public DoubleLinkedListSentinel() {
        head = new Node(null, 666, null);
        tail = new Node(null, 888, null);
        tail.prev = head;
        head.next = tail;
    }

    /**
     * 根据索引位置查找结点
     *
     * @param index
     * @return
     */
    private Node findeNode(int index) {
        int i = -1;
        for (Node p = head; p != tail; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    /**
     * 头部插入节点 调用insert方法
     *
     * @param index
     * @param value
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    /**
     * 根据索引位置插入节点
     *
     * @param index
     * @param value
     */
    public void insert(int index, int value) {
        Node prev = findeNode(index - 1);
        if (prev == null) {
            throw IllegalIndex(index);
        }
        Node next = prev.next;
        Node inserted = new Node(prev.next, value, next.prev);
        prev.next = inserted;
        next.prev = inserted;
    }

    private static IllegalArgumentException IllegalIndex(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不存在%n", index));
    }

    /**
     * 根据索引删除节点
     * @param index
     */
    public void remove(int index) {
        Node prev = findeNode(index - 1);
        //如果prev为空就是index没找到
        if(prev == null){
            throw IllegalIndex(index);
        }
        Node removed = prev.next;
        //如果删除的是尾哨兵 直接抛出
        if(removed == tail){
            throw IllegalIndex(index);
        }
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;

    }

    /**
     * 删除首节点
     * @param index
     */
    public void removeFirst(){
        remove(0);
    }

    /**
     * 尾部添加节点
     * @param value 节点值
     */
    public void addLast(int value){
        Node last = tail.prev;
        Node addNode = new Node(last, value, tail);
        last.next = addNode;
        tail.prev = addNode;
    }

    /**
     * 删除尾部节点
     */
    public void removeLast() {
        Node removed = tail.prev;
        if(removed == head){
            throw IllegalIndex(0);
        }
        Node prev = removed.prev;
        prev.next = tail;
        tail.prev = prev;
    }
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = tail.prev;
            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }
}
